﻿/*----------------------------------------------------------------
//Copyright (C) 2012 赵国伟
//文件功能描述：员工管理客户端脚本
//创建人：赵国伟
//生成时间：2012/03/17 16:15:00
//----------------------------------------------------------------*/

var ModelOfEmployee;
var EmployeeID = 0;
var CheckOptionOfRoles;
var AjaxServerUrl = "/Manage/AS/EmployeesCommon.aspx";

function Init() {
    EmployeeID = 0;
}

function CheckForm() {
    if ($("#txtEmployeeName").val() == "") {
        alert("系统提示：请输入用户名！");
        $("#txtEmployeeName").focus();
        return false;
    }
    if (EmployeeID == 0 && $("#txtPassWord").val() == "") {
        alert("系统提示：请输入登录密码！");
        $("#txtPassWord").focus();
        return false;
    }

    if ($("#txtTrueName").val() == "") {
        alert("系统提示：请输入真实姓名！");
        $("#txtTrueName").focus();
        return false;
    }
    if (GetCheckObjectValues("chkRoleID") == "") {
        alert("系统提示：请选择所属角色！");
        return false;
    }
    return true;
}

function Save() {
    $("#btnSave").attr("disabled", true);
    if (!CheckForm()) {
        $("#btnSave").attr("disabled", false);
        return;
    }
    var dataInfo = clone(ModelOfEmployee);
    dataInfo._employeeid = EmployeeID;
    dataInfo._employeename = $("#txtEmployeeName").val();
    dataInfo._password = $("#txtPassWord").val();
    dataInfo._truename = $("#txtTrueName").val();
    dataInfo._sex = $("#ddlSex").val();
    dataInfo._status = $("#ddlStatus").val();
    dataInfo._lastlogindate = "/Date(" + CurrentDateStringToUTC() + ")/";
    dataInfo._createdate = "/Date(" + CurrentDateStringToUTC() + ")/";

    var url = AjaxServerUrl + "?Action=SaveEmployee";
    var postData = "jsonData=" + JSON.stringify(eval(dataInfo));
    postData += "&roleIds=" + GetCheckObjectValues("chkRoleID");
    AjaxPostEx(url, "btnSave", postData);  //更新数据；
    $("#btnSave").attr("disabled", false);
}

function BindEmployeeInfoToForm() {
    var url = AjaxServerUrl + "?action=GetEmployee&EmployeeID=" + EmployeeID + "&tttt=" + Math.random();
    $.ajax(
    {
        type: "Get",
        url: url,
        dataType: "json",
        success: function (dataInfo) {
            if (dataInfo != null) {
                $("#txtEmployeeName").val(dataInfo.Employee._employeename);
                $("#txtTrueName").val(dataInfo.Employee._truename);
                $("#ddlSex").val(dataInfo.Employee._sex);
                $("#ddlStatus").val(dataInfo.Employee._status);
               
                //绑定角色信息；
                if (dataInfo.Roles != null) {
                    for (var i = 0; i < dataInfo.Roles.length; ++i) {
                        if ($("#chkOfRole" + dataInfo.Roles[i]._id) != null)
                            $("#chkOfRole" + dataInfo.Roles[i]._id).attr("checked", true);
                    }
                }
            }
        }
    });
}


function Add() {
    Init();
    GetPopupDiv(500, 700, "#e2e2e2", "添加员工", GetFormHTML());
}

function Edit(id) {
    Init();
    EmployeeID = id;
    GetPopupDiv(500, 700, "#e2e2e2", "编辑员工", GetFormHTML());
    BindEmployeeInfoToForm();
}
function Delete(id) {
    if (!confirm("提示：您确认要删除该员工信息吗？"))
        return;
    AjaxGet(AjaxServerUrl + "?Action=DeleteEmployee&EmployeeID=" + id);
}

function GetFormHTML() {
    var html = new Array();
    html.push("<form>");
    html.push("<table width=\"500\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\"  class=\"table\">");
    html.push("  <tr>");
    html.push("    <td colspan=\"2\">&gt;&gt;员工信息</td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td width=\"76\" align=\"right\">用户名：</td>");
    html.push("    <td width=\"424\"><input name=\"txtEmployeeName\" type=\"text\" id=\"txtEmployeeName\" size=\"20\" class=\"border6\">");
    html.push("      <span class=\"red\">*</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">密码：</td>");
    html.push("    <td><input name=\"txtPassWord\" type=\"password\" id=\"txtPassWord\" size=\"20\" class=\"border6\">");
    html.push("      <span class=\"red\">* 编辑的时候，如不修改请留空</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">真实姓名：</td>");
    html.push("    <td><input name=\"txtTrueName\" type=\"text\" id=\"txtTrueName\" size=\"10\" class=\"border6\">");
    html.push("      <span class=\"red\">*</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">性别：</td>");
    html.push("    <td><select name=\"ddlSex\" id=\"ddlSex\">");
    html.push("      <option value=\"男\">男</option>");
    html.push("      <option value=\"女\">女</option>");
    html.push("    </select>    </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\"  >所属角色：</td>");
    html.push("    <td align=\"left\"  >" + CheckOptionOfRoles + "");
    if (CheckOptionOfRoles == "")
        html.push("<span class=\"red\">系统还未设置任何角色信息！</span>");
    html.push("   <span class=\"red\">*</span></td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">状态：</td>");
    html.push("    <td><select name=\"ddlStatus\" id=\"ddlStatus\">");
    html.push("      <option value=\"正用\">正用</option>");
    html.push("      <option value=\"停用\">停用</option>");
    html.push("    </select>    </td>");
    html.push("  </tr>");
    html.push("  <tr>");
    html.push("    <td align=\"right\">&nbsp;</td>");
    html.push("    <td><input name=\"btnSave\" type=\"button\" id=\"btnSave\" value=\" 保 存 \" class=\"btn2\" onclick=\"Save();\">");
    html.push("    <input name=\"btnReset\" type=\"reset\" id=\"btnReset\" value=\" 重 置 \" class=\"btn2\">");
    html.push("    <input name=\"btnClose\" type=\"button\" id=\"btnClose\" value=\"关闭窗口\" onClick=\"yesOther(false)\" class=\"btn2\"></td>");
    html.push("  </tr>");
    html.push("</table>");
    html.push("</form>");

    return html.join("");

}




